package cn.medemede.servelt;

import cn.medemede.beans.Action;
import cn.medemede.factory.DAOFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class JServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/text;charset=GBK");
        String actionName=req.getParameter("actionName");
        PrintWriter out = resp.getWriter();
        System.out.println("Action="+actionName);
        Action action=null;
        if ("Log_check".equals(actionName)){
            action=DAOFactory.getLogCheck();
        }else if ("Do_join".equals(actionName)){
            action=DAOFactory.getDoJoin();
        }else if ("Do_change".equals(actionName)){
            action=DAOFactory.getDoChenge();
        }else if ("clear".equals(actionName)){
            action=DAOFactory.getClear();
        }else {
            throw new RuntimeException("请求失败！");
        }
        String forward="";
        try {
            forward=action.execute(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(forward);
        out.println(forward);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
